package com.lhf.planClass.controller;

import com.lhf.enumfile.Status;
import com.lhf.planClass.model.*;
import com.lhf.planClass.service.PlanClassVoService;
import com.lhf.util.ResultBuliderVo;
import com.lhf.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 尐痞猫
 * @date2021-06-24 10:22
 */
@RestController
@CrossOrigin
public class PlanClassVoController {
    @Autowired
    private PlanClassVoService planClassVoService;

    @PostMapping("planCategory/queryPage")
    public ResultVo queryPage(@RequestBody PlanClassSelectVo planClassSelectVo){
        return planClassVoService.queryPage(planClassSelectVo);
    }

    @PostMapping("planCategory/add")
    public ResultVo add(@RequestBody @Valid PlanCLassAddVo planClassSelectVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ResultBuliderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassVoService.add(planClassSelectVo);
    }
    @PostMapping("planCategory/update")
    public ResultVo update(@RequestBody @Valid PlanClassUpdateVo planClassUpdateVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ResultBuliderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassVoService.update(planClassUpdateVo);
    }
    @GetMapping("queryById")
    public ResultVo queryById(Integer id){
        return planClassVoService.queryById(id);
    }

    @PostMapping("planCategory/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid PlanClasUpdateStatusVo planClasUpdateStatusVo){
        return planClassVoService.updateStatus(planClasUpdateStatusVo);
    }

    @PostMapping("metadata/getPullDownList")
    public ResultVo xiala(){
        List<StatusVo> statusVos = new ArrayList<>();

        for (Status c : Status.values()) {
            statusVos.add(new StatusVo(c.getStatusName(),c.getStatus()));
        }
        return ResultBuliderVo.success(statusVos);
    }

}
